package com.itheima.case2.web;

import com.itheima.case2.pojo.vo.*;
import com.itheima.case2.service.UserService;
import com.itheima.case2.utils.BaseController;
import com.itheima.case2.utils.BeansFactory;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//这里使用目录匹配，找下面方法
@WebServlet("/user/*")
public class UserServlet extends BaseServlet{

    //分页查询用户和对应角色信息
    public void queryUserAndRoleByPage(HttpServletRequest request, HttpServletResponse response){
        try {
            //使用工具类获取前端传入的数据
            QueryPageBean pageBean = BaseController.parseJSON2Object(request, QueryPageBean.class);
            //使用工厂创建对象并获取
            UserService userService = BeansFactory.getInstance("userService");
            //调用对应分页查询方法
            PageResult pageResult = userService.queryUserAndRoleByPage(pageBean);
            //创建Result对象并返回给前端
            Result result = new Result(true, "用户页面分页数据查询成功", pageResult);
            BaseController.printResult(response,result);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                Result result = new Result(false, "用户页面分页数据查询失败");
                BaseController.printResult(response,result);
            } catch (Exception ioException) {
                ioException.printStackTrace();
            }
        }
    }

    //修改用户和对应角色信息
    public void updateUserAndRole(HttpServletRequest request, HttpServletResponse response){
        try {
            //使用工具类获取前端传入的数据
            UpdateUser updateUser = BaseController.parseJSON2Object(request, UpdateUser.class);
            //使用工厂创建对象并获取
            UserService userService = BeansFactory.getInstance("userService");
            userService.updateUserAndRole(updateUser);
            Result result = new Result(true, "用户信息修改成功！");
            BaseController.printResult(response,result);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                Result result = new Result(false, "用户信息修改失败！");
                BaseController.printResult(response,result);
            } catch (Exception ioException) {
                ioException.printStackTrace();
            }
        }
    }

    //添加用户和对应角色信息
    public void insertUserAndRole(HttpServletRequest request, HttpServletResponse response){
        try {
            //使用工具类获取前端传入的数据
            AddUser addUser = BaseController.parseJSON2Object(request, AddUser.class);
            //使用工厂创建对象并获取
            UserService userService = BeansFactory.getInstance("userService");
            userService.insertUserAndRole(addUser);
            Result result = new Result(true, "恭喜你，用户添加成功！");
            BaseController.printResult(response,result);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                Result result = new Result(false, "用户添加失败！");
                BaseController.printResult(response,result);
            } catch (Exception ioException) {
                ioException.printStackTrace();
            }
        }
    }

    //删除用户和对应角色信息
    public void deleteById(HttpServletRequest request, HttpServletResponse response){
        try {
            //获取前端参数
            String userId = request.getParameter("userId");
            //使用工厂创建对象并获取
            UserService userService = BeansFactory.getInstance("userService");
            userService.deleteById(userId);
            Result result = new Result(true, "恭喜你，用户删除成功！");
            BaseController.printResult(response,result);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                Result result = new Result(false, "用户删除失败！");
                BaseController.printResult(response,result);
            } catch (Exception ioException) {
                ioException.printStackTrace();
            }
        }



    }



}
